package com.cn.shuangzi.tt;

import android.app.Application;

import com.bytedance.applog.AppLog;
import com.bytedance.applog.InitConfig;
import com.bytedance.applog.util.UriConstants;
import com.bytedance.sdk.dp.DPSdk;
import com.bytedance.sdk.dp.DPSdkConfig;
import com.bytedance.sdk.dp.DPWidgetBannerParams;
import com.bytedance.sdk.dp.DPWidgetBubbleParams;
import com.bytedance.sdk.dp.DPWidgetDrawParams;
import com.bytedance.sdk.dp.DPWidgetGridParams;
import com.bytedance.sdk.dp.DPWidgetInnerPushParams;
import com.bytedance.sdk.dp.DPWidgetNewsParams;
import com.bytedance.sdk.dp.DPWidgetTextChainParams;
import com.bytedance.sdk.dp.DPWidgetVideoCardParams;
import com.bytedance.sdk.dp.DPWidgetVideoSingleCardParams;
import com.bytedance.sdk.dp.IDPWidget;
import com.bytedance.sdk.dp.IDPWidgetFactory;
import com.cn.shuangzi.SZApp;
import com.cn.shuangzi.util.SZUtil;

import org.json.JSONObject;

public final class DPManager {
    private static volatile DPManager sInstance;
    private int initTimes;
    private OnDPSdkInitListener onDPSdkInitListener;
    private boolean isInitSuccess;

    public static DPManager getInstance() {
        if (sInstance == null) {
            synchronized (DPManager.class) {
                if (sInstance == null) {
                    sInstance = new DPManager();
                }
            }
        }
        return sInstance;
    }

    private DPManager() {
        initTimes = 1;
    }

    public void init(Application context, String appContentId, String sdkSettingFileName) {
        init(context,appContentId,sdkSettingFileName,null);
    }

    public void init(Application context, String appIdTTContent, String sdkSettingFileName, OnDPSdkInitListener onDPSdkInitListener) {
        SZUtil.log("==========内容DPSdk开始初始化==========");
        this.onDPSdkInitListener = onDPSdkInitListener;
        isInitSuccess = false;
        //先初始化applog，一定要在DPSdk之前进行初始化
        final InitConfig appLogConfig = new InitConfig(appIdTTContent, SZUtil.getChannel(context));
        appLogConfig.setUriConfig(UriConstants.DEFAULT);
        appLogConfig.setAbEnable(true);
        appLogConfig.setAutoStart(true);
        AppLog.init(context, appLogConfig);

        //1. 初始化，最好放到application.onCreate()执行
        //2. 【重要】如果needInitAppLog=false，请确保AppLog初始化一定要在合作sdk初始化前
        final DPSdkConfig config = new DPSdkConfig.Builder()
                .debug(SZApp.getInstance().isDebug())
                .needInitAppLog(false)
                .initListener(new DPSdkConfig.InitListener() {
                    @Override
                    public void onInitComplete(boolean isSuccess) {
                        //注意：1如果您的初始化没有放到application，请确保使用时初始化已经成功
                        //     2如果您的初始化在application
                        //isSuccess=true表示初始化成功
                        //初始化失败，可以再次调用初始化接口（建议最多不要超过3次)
                        if (!isSuccess && initTimes < 3) {
                            initTimes++;
                            init(context, appIdTTContent, sdkSettingFileName, onDPSdkInitListener);
                        }
                        if (isSuccess) {
                            isInitSuccess = true;
                            if (onDPSdkInitListener != null) {
                                onDPSdkInitListener.onInitDone(true);
                            }
                        } else {
                            if (initTimes >= 3) {
                                if (onDPSdkInitListener != null) {
                                    onDPSdkInitListener.onInitDone(false);
                                }
                            }
                        }
                        SZUtil.log("穿山甲内容sdk初始化 result=" + isSuccess + "||初始化次数：" + initTimes);
                    }
                })
                .build();
        DPSdk.init(context, sdkSettingFileName, config);
    }

    public boolean isInitSuccess() {
        return isInitSuccess;
    }

    public void resetInitTimes() {
        initTimes = 1;
    }

    public IDPWidget buildDrawWidget(DPWidgetDrawParams params) {
        if (!isInitSuccess()) {
            return null;
        }
        //创建draw视频流组件
        return getFactory().createDraw(params);
    }

    public IDPWidget buildGridWidget(DPWidgetGridParams params) {
        if (!isInitSuccess()) {
            return null;
        }
        //创建宫格组件
        return getFactory().createGrid(params);
    }

    public IDPWidget buildDoubleFeedWidget(DPWidgetGridParams params) {
        if (!isInitSuccess()) {
            return null;
        }
        //创建双Feed组件
        return getFactory().createDoubleFeed(params);
    }

    public IDPWidget buildNewsTabsWidget(DPWidgetNewsParams params) {
        if (!isInitSuccess()) {
            return null;
        }
        //创建多频道新闻组件
        return getFactory().createNewsTabs(params);
    }

    public IDPWidget buildNewsOneTabWidget(DPWidgetNewsParams params) {
        if (!isInitSuccess()) {
            return null;
        }
        //创建单列表新闻组件
        return getFactory().createNewsOneTab(params);
    }

    public void loadVideoCard(DPWidgetVideoCardParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadVideoCard(params, callback);
    }

    public void loadSmallVideoCard(DPWidgetVideoCardParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadSmallVideoCard(params, callback);
    }

    public void loadCustomVideoCard(DPWidgetVideoCardParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadCustomVideoCard(params, callback);
    }

    public void loadVideoSingleCard(DPWidgetVideoSingleCardParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadVideoSingleCard(params, callback);
    }

    public void loadVideoSingleCard4News(DPWidgetVideoSingleCardParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadVideoSingleCard4News(params, callback);
    }

    public void loadTextChain(DPWidgetTextChainParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadTextChain(params, callback);
    }

    public void loadBubble(DPWidgetBubbleParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadBubble(params, callback);
    }

    public void loadBanner(DPWidgetBannerParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadBanner(params, callback);
    }

    public void loadInnerPush(DPWidgetInnerPushParams params, IDPWidgetFactory.Callback callback) {
        getFactory().loadInnerPush(params, callback);
    }

    public void enterNewsDetail(DPWidgetNewsParams params, long groupId, String data) {
        getFactory().enterNewsDetail(params, groupId, data);
    }

    public void loadPush(DPWidgetNewsParams params) {
        getFactory().pushNews(params);
    }

    public void uploadLog(String category, String event, JSONObject json) {
        getFactory().uploadLog(category, event, json);
    }

    private IDPWidgetFactory getFactory() {
        //一定要初始化后才能调用，否则会发生异常问题
        return DPSdk.factory();
    }

}
